<template>
  <div class="app-container">
    <el-upload
      ref="upload"
      :auto-upload="false"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :limit="1"
      :file-list="fileList"
      :action="dev_url"
      :on-success="handleSuccess"
      name="file"
      class="upload-demo"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
      <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
    </el-upload>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dev_url: process.env.BASE_API + '/eduservice/subject/addSubject',
      fileList: [
      ]
    }
  },
  methods: {
    handleSuccess(response, file, fileList) {
      console.log(response)
      if (response.code === 20000) {
        this.$message({
          type: 'success',
          message: response.message
        })
        // 跳转路由.
        this.$router.push({ path: '/subject/list' })
      } else {
        this.$message({
          type: 'error',
          message: response.message
        })
      }
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>

<style>

</style>
